local duobi = fk.CreateSkill{
  name = "xiaobai__duobi",
  tags = {
    Skill.Limited
  }
}

Fk:loadTranslationTable{
  ["xiaobai__duobi"] = "踱庇",
  [":xiaobai__duobi"] = "限定技，你可以将所有手牌当可对其他角色使用的【桃】使用并与目标各摸两张牌；"..
    "该角色造成伤害令其他角色进入濒死后，你重置此技能。",
  ["#xiaobai__duobi"] = "踱庇：你可以将所有手牌当【桃】使用",
  ["#xiaobai__duobi-active"] = "踱庇：你可以将所有手牌当【桃】使用（可以指定其他角色为目标）",
  ["@@xiaobai__duobi"] = "踱庇",
}

duobi:addEffect("active", {
  anim_type = "big",
  prompt = "#xiaobai__duobi-active",
  can_use = function (self, player)
    return player:usedSkillTimes(duobi.name, Player.HistoryGame) == 0 and not player:isKongcheng()
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    local card = Fk:cloneCard("peach")
    card:addSubcards(player:getCardIds("h"))
    card.skillName = duobi.name
    return #selected == 0 and to_select.hp < to_select.maxHp and player:canUseTo(card, to_select)
  end,
  card_filter = Util.FalseFunc,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local card = Fk:cloneCard("peach")
    card:addSubcards(player:getCardIds("h"))
    card.skillName = duobi.name
    room:useCard{
      card = card,
      from = player,
      tos = effect.tos,
    }
  end,
})

duobi:addEffect(fk.AskForCardUse, {
  anim_type = "big",
  can_trigger = function (self, event, target, player, data)
    local card = Fk:cloneCard("peach")
    card:addSubcards(player:getCardIds("h"))
    card.skillName = duobi.name
    return (not data.isResponse) and player:hasSkill(duobi.name) and player:usedSkillTimes(duobi.name, Player.HistoryGame) == 0 and Exppattern:Parse(data.pattern):match(card)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = duobi.name,
      prompt = "#xiaobai__duobi"
    })
  end,
  on_use = function (self, event, target, player, data)
    local card = Fk:cloneCard("peach")
    card:addSubcards(player:getCardIds("h"))
    card.skillName = duobi.name
    data.result = {
      from = player,
      card = card
    }
    return true
  end
})

duobi:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return data.card.name == "peach" and table.contains(data.card.skillNames, duobi.name) and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 2, duobi.name)
    room:drawCards(data.tos[1], 2, duobi.name)
    room:addTableMarkIfNeed(player, "@@xiaobai__duobi", data.tos[1].id)
  end
})

duobi:addEffect(fk.Damage, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local damageEvent = room.logic:getCurrentEvent():findParent(GameEvent.Damage, true)
    return damageEvent and player:isAlive() and player:usedSkillTimes(duobi.name, Player.HistoryGame) > 0 
      and table.find(player:getTableMark("@@xiaobai__duobi"), function (pid)
        return pid == target.id
      end) and #room.logic:getEventsOfScope(GameEvent.Dying, 1, function (e)
        return e:findParent(GameEvent.Damage, true).id == damageEvent.id
      end, Player.HistoryPhase) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if not player:hasSkill(duobi.name) then
      if player:hasSkill(duobi.name, true) then
        room:validateSkill(player, duobi.name)
      else
        room:handleAddLoseSkills(player, duobi.name)
        room:validateSkill(player, duobi.name)
      end
    end
    player:setSkillUseHistory(duobi.name, 0, Player.HistoryGame)
    room:removeTableMark(player, "@@xiaobai__duobi", target.id)
  end
})

return duobi